/**
 * 
 */
package com.gosophia.govDoc.action.transientDoc;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.govDoc.dto.TransientDocumentDTO;
import com.gosophia.govDoc.ws.TransientDocumentWebService;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.userManagement.dto.UserInfoDTO;
import com.gosophia.userManagement.ws.UserManagementWebService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 功能：
 * 
 * @author zhangdongsheng
 * @since 2011-2-9 Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@SuppressWarnings("serial")
@Controller("TransientDocAction_2")
@Scope("prototype")
public class TransientDocAction_2 extends ActionSupport {

    private TransientDocumentDTO transientDocumentDto;
    private String approvalPersonString;
    private Long jobId;

    // private String docPath;
    // private String docVersion;
    // private String docType;

    private String fromFlag;// 用于控制页面菜单的显示

    @Autowired
    private TransientDocumentWebService transientDocumentWebService;
    @Autowired
    private UserManagementWebService userManagementWebService;

    private final Logger logger = Logger.getLogger(this.getClass());

    @Override
    public String execute() throws Exception {
        UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
                .getContext().getAuthentication().getPrincipal();
        // 得到当前用户
        Long creatorId = userDetails.getUserAccountDTO().getUserInfoDTO()
                .getUserInfoId();
        transientDocumentDto.setJobId(jobId);
        if (transientDocumentDto.getJobId() != null) {
            fromFlag = "task";
        } else {
            fromFlag = "bussiness";
        }
        transientDocumentWebService
                .saveOrUpdateTransientDocument(transientDocumentDto);
        logger.debug("文档起草第二步骤保存信息：" + transientDocumentDto.toString());

        approvalPersonString = getAllApprovalPersionStr(creatorId);// 用于跳转页面的显示
        transientDocumentDto = transientDocumentWebService
                .findTranDocByCreatorAndJobId(creatorId, jobId);
        return SUCCESS;
    }

    private String getAllApprovalPersionStr(long espectUserId) {
        // 得到所有有账号信息的用户
        List<UserInfoDTO> userInfos = userManagementWebService
                .findAllUserInfoHasAccount();
        List<UserInfoDTO> toDel = new ArrayList<UserInfoDTO>();
        if (userInfos != null && userInfos.size() != 0) {
            for (UserInfoDTO u : userInfos) {
                if (u.getUserInfoId().equals(espectUserId)) {
                    toDel.add(u);
                }
            }
            userInfos.removeAll(toDel);
        }
        String allApprovators = JSONArray.fromObject(userInfos).toString()
                .replace("\"", "\\\"");
        return allApprovators;
    }

    // ~ getter setter
    public TransientDocumentDTO getTransientDocumentDto() {
        return transientDocumentDto;
    }

    public void setTransientDocumentDto(
            TransientDocumentDTO transientDocumentDto) {
        this.transientDocumentDto = transientDocumentDto;
    }

    public String getApprovalPersonString() {
        return approvalPersonString;
    }

    public void setApprovalPersonString(String approvalPersonString) {
        this.approvalPersonString = approvalPersonString;
    }

    public String getFromFlag() {
        return fromFlag;
    }

    public void setFromFlag(String fromFlag) {
        this.fromFlag = fromFlag;
    }

    public Long getJobId() {
        return jobId;
    }

    public void setJobId(Long jobId) {
        this.jobId = jobId;
    }

    // public String getDocPath() {
    // return docPath;
    // }
    //
    // public String getDocVersion() {
    // return docVersion;
    // }
    //
    // public String getDocType() {
    // return docType;
    // }
    //
    // public void setDocPath(String docPath) {
    // this.docPath = docPath;
    // }
    //
    // public void setDocVersion(String docVersion) {
    // this.docVersion = docVersion;
    // }

    // public void setDocType(String docType) {
    // this.docType = docType;
    // }

}
